'use strict';

let
    router = require('koa-router')(),
    fs = require('fs'),
    path = require('path');

function searchForRouterDefineFile(){
    let file = fs.readdirSync(__dirname);
    return file.filter(name=>{
        return name.endsWith('.js') && name !== 'index.js';
    })
};

function registerAllRouterDefineFile(files){
    files.forEach(ele=>{
        let tmpFile = require(path.join(__dirname,ele));
        for(let key in tmpFile){
            let type = tmpFile[key][0];
            let value = tmpFile[key][1];

            if(type === 'get'){
                router.get(key,value);
            }else if(type === 'post'){
                router.post(key,value);
            }
        }
    })
};

module.exports = function(){
    let c = __dirname;

    let curr = searchForRouterDefineFile(c);

    registerAllRouterDefineFile(curr);

    return router.routes();
}